package service
{
	import mx.collections.ArrayCollection;
	
	public class GetAllProjectsRequest {
		private var responseCallback:Function;
		private var bambooService:BambooService;
		
    	private var projects:ArrayCollection;

		private var username:String;
		private var password:String;
		
		private var auth:String;
		
		public function GetAllProjectsRequest( username:String, password:String, bambooService:BambooService, responseCallback:Function) {
			this.username = username;
			this.password = password;
			this.bambooService = bambooService;
			this.responseCallback = responseCallback;
		}
		
		public function send():void {
			bambooService.requestAuthToken( username, password, onRequestAuthTokenSuccess, onFailure );
		}
		
		private function onRequestAuthTokenSuccess(auth:String):void { 
			this.auth = auth;
			bambooService.loadAllProjects( auth, onLoadAllProjects );
		} 

		private function onLoadAllProjects( projects:ArrayCollection ):void {
			this.projects = projects;
			bambooService.releaseAuthToken( auth, onExecuteSuccess );
		}
		
		private function onFailure( e:Error ):void {
			trace( "error occured: "+e.message );
		}
		private function onExecuteSuccess():void {
			responseCallback( this.projects );
		}
	}
}